package com.exp.bookhouse.controller;

import com.exp.bookhouse.pojo.vo.ShopCartInfo;
import com.exp.bookhouse.pojo.vo.StockVo;
import com.exp.bookhouse.service.BookService;
import com.exp.bookhouse.service.ShopCartService;
import com.exp.bookhouse.util.ApiJSONResult;
import com.exp.bookhouse.util.RedisUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "购物车管理接口",tags = "购物车操作")
@RestController
@RequestMapping("/cart")
public class ShopCartController {

    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private BookService bookService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "查询缓存中购物车信息")
    @GetMapping("/query/{id}")
    public ApiJSONResult queryShopCart(@PathVariable("id") Long id){
        Object cart = shopCartService.queryShopCart(id);
        if(cart == null){
            return ApiJSONResult.ok();
        }
        ShopCartInfo shopCartInfo = (ShopCartInfo)cart;
        List<StockVo> stockVos = bookService.queryStockById(shopCartInfo.getShopList());
        shopCartInfo.setStockVos(stockVos);
        return ApiJSONResult.ok(shopCartInfo);
    }

    @ApiOperation(value = "缓存购物车信息")
    @PostMapping("/save")
    public ApiJSONResult saveShopCart(@ApiParam(value="购物车信息", required = true) @RequestBody ShopCartInfo shopCartInfo){
        boolean b = shopCartService.saveShopCart(shopCartInfo);
        if(!b){
            return ApiJSONResult.errorMsg("缓存失败");
        }
        return ApiJSONResult.ok();
    }

}
